'use strict';

const Controller = require('egg').Controller;

class UserSettController extends Controller {
  async headImg_upload() {
    const {
      ctx,
    } = this;
    const stream = await ctx.getFileStream();
    const id = ctx.params.id;
    const result = await ctx.service.userSett.img_upload(stream, id);
    ctx.body = result;
  }
  async setNickName() {
    const { ctx } = this;
    const newNickNameInfo = ctx.request.body;
    const result = await ctx.service.userSett.setNickName(newNickNameInfo);
    ctx.body = result;
  }
  async address() {
    const { ctx } = this;
    const code = ctx.params.code;
    let { data: address } = await ctx.curl('http://apis.juhe.cn/xzqh/query?key=11a0d7c5bfb201d98621857a1f5a78b7&fid=' + code);
    address = JSON.parse(address.toString());
    if (address.reason === 'success') {
      ctx.body = {
        status: 200,
        data: address.result,
      };
      return;
    }
    ctx.body = {
      status: 400,
      data: '获取失败,请重新再试',
    };
  }
  async setOther() {
    const { ctx } = this;
    const userInfo = ctx.request.body;
    const result = await ctx.service.userSett.setOther(userInfo);
    ctx.body = result;
  }
}

module.exports = UserSettController;
